﻿using System;

using SwAddinHelpers.Base;
using SwAddinHelpers.Contracts;
using SwAddinHelpers.Attributes;
using SwAddinHelpers.Sinks;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace ExternalControls
{
    [ComVisible(true)]
    [ProgId(PROGID)]
    [Export(typeof(IExternalUserControl))]
    [Export(typeof(IExternalCallback))]
    [HostedAsTaskpane]
    [CommandGroupDefinition("Group1",
                SolidWorks.Interop.swconst.swDocumentTypes_e.swDocPART | 
                SolidWorks.Interop.swconst.swDocumentTypes_e.swDocASSEMBLY | 
                SolidWorks.Interop.swconst.swDocumentTypes_e.swDocDRAWING | 
                SolidWorks.Interop.swconst.swDocumentTypes_e.swDocNONE | 
                SolidWorks.Interop.swconst.swDocumentTypes_e.swDocSDM ,
            true,
            false,
            null)]
    [CommandGroupDefinition("Group2",
            SolidWorks.Interop.swconst.swDocumentTypes_e.swDocPART |
                SolidWorks.Interop.swconst.swDocumentTypes_e.swDocASSEMBLY |
                SolidWorks.Interop.swconst.swDocumentTypes_e.swDocDRAWING |
                SolidWorks.Interop.swconst.swDocumentTypes_e.swDocNONE |
                SolidWorks.Interop.swconst.swDocumentTypes_e.swDocSDM,
            true,
            true,
            null)]
    public class ExternalTaskPaneControlTemplate1: TaskPaneControlBase, IExternalUserControl, IExternalCallback
    {
        public const string PROGID = "SwExternalControls.ExternalTaskPaneControlTemplate1";

        public ExternalTaskPaneControlTemplate1()
        {
            InitializeComponent();
            
        }

        public dynamic ExternalUserControl
        {
            get { return this; }
        }

        public dynamic ExternalCallbackContainer
        {
            get { return this; }
        }

        public ISwAddinBase OwnerAddin
        {
            get { return (ISwAddinBase)this.GetType().GetField("OwnerAddin").GetValue(this); }
        }

        [SinkMethod(enumEventSink.SW_ActiveModelDocChangeNotify,true)]
        private void ActiveModelDocChangedNotification(object sender, EventArgs e)
        {
            _OwnerHost.OwnerAddin.Debug.Manager.PostEvent();
        }

        [SinkMethod(enumEventSink.Document_DestroyNotify, true)]
        private void Document_DestroyNotify(object sender, SolidWorksEventArgs e)
        {
            _OwnerHost.OwnerAddin.Debug.Manager.PostEvent();
        }

        [SinkMethod(enumEventSink.Document_FileSaveNotify, true)]
        private void Document_FileSaveNotify(object sender, SolidWorksEventArgs e)
        {
            _OwnerHost.OwnerAddin.Debug.Manager.PostEvent();
        }

        [CommandItemCallback("Group1","DynamicCommand1", "CommandEnable1", "Group1_dynamicCommand1")]
        public void CommandCallback1()
        {
            // do some work when command is clicked
            System.Windows.Forms.MessageBox.Show("hello from command");
        }

        public int CommandEnable1()
        {
            /*
             * 0 - deselect and disable
             * 1 - deselect and enable, default
             * 2 - selects and disables
             * 3 - selects and enables
             * 4 - hides
            */

            return 1;
        }

        [CommandItemCallback("Group2", "DynamicCommand1", null, "Group2_DynamicCommand1")]
        public void CommandCallback2()
        {
            // do some work when command is clicked
        }
        
        #region UI Elements
        private System.Windows.Forms.Button button1;
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(33, 38);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 60);
            this.button1.TabIndex = 0;
            this.button1.Text = "External Task Pane Control";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Class1
            // 
            this.Controls.Add(this.button1);
            this.Name = "ExternalTaskPaneTemplate1";
            this.ResumeLayout(false);

        }
        #endregion
        
       
    }
}
